﻿using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace Red.Controls.Converters
{
    /// <summary>
    /// This converter is used to set node texts foreground colors.
    /// It adjusts colors to keep high contrast between background and foreground.
    /// It is done to provide full readability despite the background color.
    /// </summary>
    public class HighContrastColorToBrushConverter : IValueConverter
    {
        public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
        {
            var backgroundBrushColor = ( Color ) value;
            var Y = 0.2126 * backgroundBrushColor.ScR + 0.7152 * backgroundBrushColor.ScG + 0.0722 * backgroundBrushColor.ScB;
            return Y > 0.4 ? Brushes.Black : Brushes.White;
        }

        public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
        {
            throw new NotImplementedException();
        }
    }
}
